/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @package    Feather-CM
 * @subpackage Javascript
 * @copyright  Copyright (c) 2006, Julien PORTALIER
 * @link       http://portalier.com/feather
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

var Toolbar = {};

Toolbar.Base = new Class(
{
	initialize: function(attachTo, options)
	{
		this.buttons = this.buttons || [];
		
		this.options = Object.extend({
			className: 'toolbar',
			insert:    'Before'
		}, options || {});
		
		// creation
		this.create();
		
		// events
		this.toolbar.addEvent('click', this.action.bindWithEvent(this));
		
		// injects in DOM
		this.toolbar['inject' + this.options.insert](attachTo);
	},
	
	addButton: function(_id, _title, content)
	{
		this.toolbar.appendChild(
			new DOM('a', {href: '#', id: _id, title: _title}, content || _id)
		);
	},
	
	addSeparator: function()
	{
		this.toolbar.appendChild(new DOM('span', {'class': 'separator'}));
	},
	
	create: function()
	{
		this.toolbar = new DOM('div', {'class': this.options.className});
		this.buttons.forEach(function(bt)
		{
			if (bt.length > 0)
				this.addButton(bt[0], bt[1]);
			else
				this.addSeparator();
		}, this);
	},
	
	action: function(evt)
	{
		evt.stop();
		var el = evt.target;
		while(el.getTag() != 'a')
		{
			if (el.hasClass('toolbar'))
				return;
			el = el.parentNode;
		}
		this['cb_' + el.id]();
	}
});

Toolbar.Text = Toolbar.Base.extend(
{
	initialize: function(textarea, options)
	{
		this.parent(textarea, options);
		this.textarea = $(textarea);
		this.textarea.addEvent('keypress', this.onShortcut.bindWithEvent(this));
	},
	
	onShortcut: function(evt)
	{
		if (evt.control && !evt.alt)
		{
			this.buttons.each(function(bt)
			{
				if (bt.length > 2 && bt[2] == evt.key)
				{
					this['cb_' + bt[0]]();
					evt.stop();
					$break();
				}
			}, this);
		}
	},
	
	// text selection
	
	getSelection: function()
	{
		if (document.selection)
		{
			var sel = document.selection.createRange();
			return (sel.parentElement && sel.parentElement().id != this.textarea.id) ? null : sel.text;
		}
		else if (this.textarea.setSelectionRange)
		{
			var nStart = this.textarea.selectionStart;
			var nEnd   = this.textarea.selectionEnd;
			return this.textarea.value.substring(nStart, nEnd);
		}
	},
	
	replaceSelection: function(str)
	{
		this.textarea.focus();
		
		if (document.selection)
		{
			var sel = document.selection.createRange();
			if (sel.parentElement().id != this.textarea.id)
				return;
			sel.text = str;
		}
		else if (this.textarea.setSelectionRange)
		{
			var scrollPos = this.textarea.scrollTop;
			var nStart    = this.textarea.selectionStart;
			var nEnd      = this.textarea.selectionEnd;
			var pos       = nStart + str.length;
			
			this.textarea.value = this.textarea.value.substring(0, nStart) + str + this.textarea.value.substring(nEnd);
			this.textarea.setSelectionRange(pos, pos);
			this.textarea.scrollTop = scrollPos;
		}
	},
	
	encloseSelection: function(open, close)
	{
		var text = open + this.getSelection() + (close || open);
		this.replaceSelection(text);
	},
	
	replaceInSelection: function(regexp, str, before)
	{
		var text = str + this.getSelection().replace(regexp, (before || '') + str);
		this.replaceSelection(text);
	}
});